﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Eeml.Core
{
    public class Environment
    {
        #region Fields

        private string _title;
        private string _creator;
        private int _id;
        private Uri _feed;
        private Location _location;
        private IEnumerable<EemlData> _data;
        private DateTime _updated;

        #endregion


        #region Constructors

        public Environment()
        {
            _updated = System.DateTime.Now;
            _id = new Random().Next();
        }

        #endregion


        #region Properties

        public string Title
        {
            get
            {
                return _title;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new ArgumentException("Title");
                }

                if (_title == value)
                {
                    return;
                }

                _title = value;
            }
        }

        public Uri Feed
        {
            get
            {
                return _feed;
            }

            set
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }

                _feed = value;
            }
        }

        public Location Location
        {
            get
            {
                if (_location == null)
                {
                    _location = new Location();
                }
                return _location;
            }
        }

        public IEnumerable<EemlData> Data
        {
            get
            {
                if (_data == null)
                {
                    _data = new List<EemlData>();
                }
                return _data;
            }
        }

        public DateTime Updated
        {
            get
            {
                return _updated;
            }
        }

        //TODO Verify this behaviour against the specification
        public string Creator
        {
            get
            {
                if (_creator == null)
                {
                    _creator = "";
                }
                return _creator;
            }

            set
            {
                if (!string.IsNullOrEmpty(_creator))
                {
                    return;
                }
                _creator = value;
            }
        }

        //TODO Properly initialize id
        public int Id
        {
            get
            {
                return _id;
            }
        }

        #endregion


        #region Methods

        public void AddLocation(Location location)
        {
            if (location == null)
            {
                throw new ArgumentNullException("location");
            }

            _location = location;
        }

        public void AddData(IEnumerable<EemlData> data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            _data = data;
        }

        #endregion
    }
}
